<?php
/** 
 * Created by RisingSun. 
 * User: MarkSpace 
 * Date: 20210822
 * Time: 11:05 
 * 基础设置
 */
namespace app\admin\model;

use app\admin\model\Model;

class Basicset extends Model{

    private $exchange = 'settle_exchange';
    private $exchangelog = 'settle_exchangelog';
    private $basics = 'sys_basics';
    private $custer = 'sys_custer';

    // private $error=['code'=>0,'msg'=>'fail'];
    // private $success=['code'=>200,'msg'=>'success'];

    /**
     * 汇率列表
     * @param array $accountlist
     * @param array $where
     */
    public function changerate($where = '', $page = 1, $page_size = 10)
    {
        //部门数据
        $rate['datalist'] = Model::name($this->exchange)
            ->alias('a')
            ->field(' a.*')
            ->where($where)
            ->limit(($page - 1) * $page_size, $page_size)
            ->order('a.id desc')
            ->select()
            ->toArray();
        //数据总数
        $rate['total'] = Model::name($this->exchange)
            ->alias('a')
            ->where($where)
            ->count('a.id');
        return $rate;
    }

    /**
     * 汇率列表
     * @param array $accountlist
     * @param array $where
     */
    public function rateinfo($where = '', $file = '*')
    {
        $rateinfo = Model::name($this->exchange)
            ->field($file)
            ->where($where)
            ->find();
        return $rateinfo;
    }

    /**
     * 汇率信息保存
     * @param array $ratesave 
     * @param array $where 
     */ 
    public function ratesave($data)
    {
        $id = !empty($data['id'])?$data['id']:'';
        unset($data['id']);
        $data['updatetime']=date('Y-m-d H:i:s',time());
        switch (true) {
            case !empty($id):
                if($data['defualt']==1){
                    Model::name($this->exchange)->where(' defualt = 1')->update(['defualt'=>0]);
                }
                Model::name($this->exchange)->where(' id=' . $id)->update($data);
                return true;
                break;
            default:
                Model::name($this->exchange)->insert($data);
                return true;
                break;
        }
        return false;
    }

    /**
     * 汇率删除
     * @param array $accountdel
     * @param array $where
     */
    public function ratedel($data)
    {
        if ($data) {
            Model::name($this->exchange)->where('id=' . $data['id'])->delete();
            return true;
        }
        return false;
    }

    /**
     * 基础信息
     * @param array $list 
     * @param array $where 
     */ 
    public function list($where=null,$page=1,$page_size=10){
        //列表
        $list['datalist']= Model::name($this->basics)
            ->where($where)
            ->limit(($page-1)*$page_size,$page_size)
            ->order('id asc')
            ->select()
            ->toArray();
        //数据总数
        $list['total']=Model::name($this->basics)
            ->where($where)
            ->count('id');
        return $list;
    }

    /**
     * 基础信息设置
     * @param array $basicsset 
     * @param array $where 
     */	
    public function basicset($id=null){
        $info=Model::name($this->basics)->where([
            'id'=>$id
        ])->find();
        switch (true) {
            case $info['type']==1:
                $data=json_decode($info['content'],true);
                $data['type']=$info['type'];
                $data['id']=$info['id'];
                $data['status']=$info['status'];
                break;
            default:
                $data['content']=json_decode($info['content'],true);
                $data['notes']=!empty($info['notes'])?json_decode($info['notes'],true):[];
                foreach ($data['content'] as $key => $value) {
                    $data['notes'][$key]=isset($data['notes'][$key])?$data['notes'][$key]:'';
                }
                $data['id']=$info['id'];
                $data['name']=$info['name'];
                $data['type']=$info['type'];
                $data['keyname']=$info['keyname'];
                $data['status']=$info['status'];
                break;
        }
        unset($info);
        return $data;
    }
    /**
     * 基础信息设置
     * @param array $basicsset 
     * @param array $where 
     */ 
    public function release($id=null){
        $info=Model::name($this->basics)->where([
            'id'=>$id
        ])->find();
        switch (true) {
            case $info['reltype']==1:
                $data['content']=json_decode($info['content'],true);
                $data['id']=$info['id'];
                $data['keyname']=$info['keyname'];
                $data['status']=$info['status'];
                break;
            default:
                $data['content']=json_decode($info['content'],true);
                $data['notes']=!empty($info['notes'])?json_decode($info['notes'],true):[];
                foreach ($data['content'] as $key => $value) {
                    $data['notes'][$key]=isset($data['notes'][$key])?$data['notes'][$key]:'';
                }
                $data['id']=$info['id'];
                $data['keyname']=$info['keyname'];
                $data['status']=$info['status'];
                break;
        }
        Model::name($this->basics)->where(['id'=>$id])->update(['effect'=>1]);
        unset($info);
        return $data;
    }

    /**
     * 基础信息删除
     * @param array $del
     * @param array $where
     */
    public function del($data)
    {
        if ($data && $data['id']>1) {
            Model::name($this->basics)->where('id=' . $data['id'])->delete();
            return true;
        }
        return false;
    }

    /**
     * 基础信息保存
     * @param array $save 
     * @param array $where 
     */ 
    public function save($data)
    {
        $id = !empty($data['id'])?$data['id']:'';
        unset($data['id']);
        switch (true) {
            case !empty($id):
                foreach ($data['key'] as $key => $value) {
                    $content[$value]=$data['value'][$key];
                    $notes[$value]=$data['notes'][$key];
                }
                unset($data['key'],$data['value'],$data['notes']);
                $data['updatetime']=date('Y-m-d H:i:s');
                $data['content']=json_encode($content,JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES);
                $data['notes']=json_encode($notes,JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES);
                $data['effect']=2;
                Model::name($this->basics)
                    ->where(' id=' . $id)
                    ->update($data);
                return true;
                break;
            default:
                foreach ($data['key'] as $key => $value) {
                    $content[$value]=$data['value'][$key];
                    $notes[$value]=$data['notes'][$key];
                }
                unset($data['key'],$data['value'],$data['notes']);
                $data['updatetime']=date('Y-m-d H:i:s');
                $data['content']=json_encode($content,JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES);
                $data['notes']=json_encode($notes,JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES);
                $data['effect']=2;
                Model::name($this->basics)
                    ->insert($data);
                return true; 
                break;
        }
        return false;       
    }

	/**
     * 基础信息保存
     * @param array $basicssave 
     * @param array $where 
     */	
    public function basicssave($data)
    {
        $id = !empty($data['id'])?$data['id']:'';
        $status = !empty($data['status'])?$data['status']:1;
        unset($data['id'], $data['status']);
        $savedata=json_encode($data,JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES);
        switch (true) {
            case !empty($id):
                Model::name($this->basics)
                    ->where(' id=' . $id)
                    ->update(['updatetime'=>date('Y-m-d H:i:s'),'content'=>$savedata,'status'=>$status]);
                    return true; 
                break;
            
            default:
                return false;
                break;
        }        
    }

    /**
     * 客服列表
     * @param array $custerlist
     * @param array $where
     */
    public function custerlist($where = '', $page = 1, $page_size = 10)
    {
        //部门数据
        $custerlist['datalist'] = Model::name($this->custer)
            ->alias('a')
            ->field(' a.*')
            ->where($where)
            ->limit(($page - 1) * $page_size, $page_size)
            ->order('a.id desc')
            ->select()
            ->toArray();
        //数据总数
        $custerlist['total'] = Model::name($this->custer)
            ->alias('a')
            ->where($where)
            ->count('a.id');
        return $custerlist;
    }

    /**
     * 客服列表
     * @param array $custerinfo
     * @param array $where
     */
    public function custerinfo($where = '', $file = '*')
    {
        $custerinfo = Model::name($this->custer)
            ->field($file)
            ->where($where)
            ->find();
        $custerinfo['langinfo']=[];
        if(isset($custerinfo['kefu_urls'])){
            $custerinfo['kefu_urls']=json_decode($custerinfo['kefu_urls'],true);  
        }
        if(isset($custerinfo['titles'])&&!empty($custerinfo['titles'])){
            $custerinfo['titles']=json_decode($custerinfo['titles'],true);
            foreach ($custerinfo['titles'] as $key => $value) {
                 $custerinfo['langinfo'][$key]['titles']=$value;
                 $custerinfo['langinfo'][$key]['kefu_urls']=isset($custerinfo['kefu_urls'][$key])?$custerinfo['kefu_urls'][$key]:'';
             } 
             
        }
        unset($custerinfo['kefu_urls'],$custerinfo['titles']);
        return $custerinfo;
    }

    /**
     * 客服信息保存
     * @param array $custersave 
     * @param array $where 
     */ 
    public function custersave($data)
    {
        $id = !empty($data['id'])?$data['id']:'';
        $titles=[];
        $kefu_urls=[];
        if(isset($data['langs'])&&!empty($data['langs'])){
            foreach ($data['langs'] as $key => $value) {
                $titles[$value]=$data['titles'][$key];
            }
            foreach ($data['langs'] as $key => $value) {
                $kefu_urls[$value]=$data['kefu_urls'][$key];
            }
        };
        unset($data['id'],$data['langs']);
        $data['titles'] = json_encode($titles,JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE);
        $data['kefu_urls'] = json_encode($kefu_urls,JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE);
        switch (true) {
            case !empty($id):
                Model::name($this->custer)->where(' id=' . $id)->update($data);
                return true;
                break;
            default:
                Model::name($this->custer)->insert($data);
                return true;
                break;
        }
        return false;
    }

    /**
     * 汇率删除
     * @param array $accountdel
     * @param array $where
     */
    public function custerdel($data)
    {
        if ($data) {
            // Model::name($this->custer)->where('id=' . $data['id'])->delete();
            Model::name($this->custer)->where('id=' . $data['id'])->update(['dels' => 1, 'status' => 0]);
            return true;
        }
        return false;
    }

}